package video

import (
	"github.com/go-redis/redis"
	"log"
	lib "tacitus/library"
	db "tacitus/library/database"
	"time"
	"xorm.io/xorm"
)

var sx *xorm.Engine
var rx *xorm.Engine
var r *redis.Client
var l *log.Logger

var MaxReportInterval = 10 // 请求之间的最大间隔时间，也是缓存刷新的间隔
var ProgressStatInterval = 20 // 统计观看进度的间隔

func init() {
	sx = db.Use("site")
	rx = db.Use("resource")

	if lib.Config.Develop.Sync {
		if err := rx.Sync2(new(Video), new(Package), new(Teacher), new(Category), new(Association), new(Link)); err != nil {
			panic(err)
		}
		if err := sx.Sync2(new(Record)); err != nil {
			panic(err)
		}
	}

	r = lib.Redis
	l = lib.WorkLogger

	// 在后台运行同步缓存和统计进度
	go StartCacheSync()
	go func() {
		// 等待缓存同步结束后开始统计
		time.Sleep(time.Second*time.Duration(MaxReportInterval*2))
		StartProgressStat()
	}()
}